'##################################################
'###  Top Level (Get root Path from system env)
'##################################################
function GetEnv(envVarName)
	On Error Resume Next
	' 创建Shell对象并获取值
	Set WshShell = CreateObject("WScript.Shell")
	envValue = WshShell.Environment("PROCESS")(envVarName) ' 优先查找进程级变量

	' 如果未找到则尝试系统级变量
	If Err.Number <> 0 Or envValue = "" Then
		Err.Clear
		envValue = WshShell.Environment("SYSTEM")(envVarName)
	End If

	' 如果仍未找到则尝试用户级变量
	If Err.Number <> 0 Or envValue = "" Then
		Err.Clear
		envValue = WshShell.Environment("USER")(envVarName)
	End If

	' 检查结果并输出
	If Err.Number = 0 And envValue <> "" Then
		GetEnv=envValue
	Else
		GetEnv=""
	End If
end function

'##################################################
'##################################################
'##################################################
sub IncludeVbs(filepath)
	Dim fso, file, content
	Set fso = CreateObject("Scripting.FileSystemObject")
	' 读取functions.vbs的内容
	Set file = fso.OpenTextFile(filepath, 1) ' 1表示只读
	content = file.ReadAll
	file.Close
	' 将functions.vbs的内容载入全局作用域
	ExecuteGlobal content
end sub

'##################################################
'##################################################
'##################################################
dim root
Dim doc
Set doc = application.ActiveDocument
main
set doc = nothing
sub main()	
	on error resume next
	'// REUSE
	envVarName ="EXP_SHORTCUTS_ROOT"
	root = GetEnv(envVarName)'"C:\MentorGraphics\2005EXP\SDD_HOME\pvtk\env\"
	if root ="" then 
		msgbox "please set env value (EXP_SHORTCUTS_ROOT = ?)"
		exit sub
	end if
	
	call IncludeVbs(root & "\skill_lib.vbs")
	
	If ValidateServer(doc) = 0 Then
	    msgbox "Server validation failed."
	    exit sub
	End If	
	
	'// TODO
	dim pcbcheckmode:pcbcheckmode = root & "config\data\Net_class_color.txt"	
	'msgbox pcbcheckmode
	dim fso:set fso = createobject("scripting.filesystemobject")
	dim fo:set fo=fso.opentextfile(pcbcheckmode,1)
	dim freadall:freadall = fo.readall
	fo.close
	set fo = nothing
	set fso=nothing
	if freadall = "" then exit sub
	
	freadall = trim(freadall)
	freadall = replace(freadall,chr(13),"")
	freadall = replace(freadall,chr(9)," ")
	'msgbox freadall
	if freadall = "" then exit sub
	
	doc.ActiveView.DisplayControl.Global.MiscItems(4) = true
	doc.ActiveView.DisplayControl.Global.MiscItems(5) = false
		
	dim ncs,nc
	set ncs = doc.NetClasses("*")
	dim ncDict
	set ncDict = createobject("scripting.Dictionary")
	for each nc in ncs
		call ncDict.add(nc.name,"")
		doc.activeView.DisplayControl.Global.Net("False",nc.name)=false		
	next
	
	' paint
	dim colorPatternObj
	dim s,ls,i
	s = split(freadall,chr(10))
	ls = ubound(s)-lbound(s)+1
	for i=0 to ls-1
		s(i)=trim(s(i))
		if s(i)="### reserve" then exit sub
		
		if s(i)<>"" then
			'msgbox s(i)
			dim cs,lcs,ix
			cs = split(s(i)," ")
			lcs = ubound(cs)-lbound(cs)+1
			if lcs =3 then
				if isnumeric(cs(0))=true and cs(0)=0 then
					dim netname :netname = cstr(cs(1))
					if ncDict.exists(netname)=true then
						dim colorstr:colorstr = cstr(cs(2))
						dim r,g,b,cindex
						dim ts,lts,ixx
						ts = split(colorstr,",")
						lts = ubound(ts)-lbound(ts)+1
						if lts =3 or lts = 4 then
							if isnumeric(ts(0)) = true and isnumeric(ts(0)) = true and isnumeric(ts(0)) = true and isnumeric(ts(0)) = true then
								r = cint(ts(0)):g=cint(ts(1)):b=cint(ts(2))
								if lts =4 then cindex = cint(ts(3))
								if cindex<0 or cindex>120 then cindex = 0
								if r<0 or r>255 or g<0 or g>255 or b<0 or b>255 then
								else
									doc.activeView.DisplayControl.Global.Net("False",netname)=True	
									set colorPatternObj = application.Utility.NewColorPattern(r, g, b,cindex)
									set doc.ActiveView.DisplayControl.Global.NetColor("False",netname) = colorPatternObj
								end if
							end if
						end if	
					end if						
				end if
			end if
		end if	
	next	
	set ncDict  = nothing
end sub